/**
 * 
 */
package br.ufrgs.inf.cwsmart.management;

import java.util.List;

import br.ufrgs.inf.cwsmart.CWSMartFramework;
import br.ufrgs.inf.cwsmart.core.CWSResponse;
import br.ufrgs.inf.cwsmart.core.CWSType;
import br.ufrgs.inf.cwsmart.core.UserRequest;
import br.ufrgs.inf.cwsmart.exception.FrameworkException;

/**
 * Handles users' requests in term of triggering CWSs.
 * This module consults the repository of CWS descriptions 
 * to look for the appropriate CWSs as per these request. Once 
 * an appropriate CWS is identified, the management deploys this 
 * CWS over some computer plantform.  
 * 
 * @author Rodrigo S Lumertz
 *
 */
public abstract class Management {

	/**
	 * The constructor automatically called by the class that implements the matcher 
	 */
	public Management() {		
	}
	
	/**
	 * 
	 * 
	 * @param userRequest
	 * @return
	 */
	public CWSResponse handleUserRequest(UserRequest userRequest) {
		CWSResponse response = null;
		
		try {
			
			List<CWSDeploy> cwsSet = CWSMartFramework.getInstance().
				getRepositoryManagerInstance().getCWSSet();
			
			CWSDeploy cwsToDeploy = null;
			
			for (CWSDeploy cws : cwsSet) {
				if (this.satisfy(userRequest, cws)) {
					cwsToDeploy = cws;
					break;
				}
			}
			
			if (cwsToDeploy != null && 
					cwsToDeploy.getCWSType() == CWSType.PROACTIVE) {				
				response = this.deploy(cwsToDeploy);
				
			} else if (cwsToDeploy != null && 
					cwsToDeploy.getCWSType() == CWSType.REACTIVE) {
				response = this.build(cwsToDeploy);
				
			}
			
		} catch (FrameworkException e) {
			// TODO: criar e lancar excecao do tipo do modulo
		}
		
		return response;
	}
	
	/**
	 * 
	 * @param userRequest
	 * @param cwsDeploy
	 * @return
	 */
	protected abstract boolean satisfy(UserRequest userRequest, CWSDeploy cwsDeploy);
	
	/**
	 * 
	 * @param cws
	 * @return
	 */
	protected abstract CWSResponse deploy(CWSDeploy cws);
	
	/**
	 * 
	 * @param cws
	 * @return
	 */
	protected abstract CWSResponse build(CWSDeploy cws);
}
